<template>
    <a-dropdown>
        <div class="header-avatar" style="cursor: pointer">
            <a-avatar class="avatar" size="small" shape="circle" :src="avatarUrl"/>
            <span class="name">{{user.nickname}}</span>
        </div>
        <a-menu :class="['avatar-menu']" slot="overlay">
            <a-menu-item @click="toCenter">
                <a-icon type="user" />
                <span>个人中心</span>
            </a-menu-item>
            <a-menu-item @click="toggledSetting">
                <a-icon type="setting" />
                <span>设置</span>
            </a-menu-item>
            <a-menu-divider />
            <a-menu-item @click="logout">
                <a-icon style="margin-right: 8px;" type="poweroff" />
                <span>退出登录</span>
            </a-menu-item>
        </a-menu>
    </a-dropdown>
</template>

<script>
import { mapGetters, mapActions, mapMutations } from 'vuex'
import { Avatar as AAvatar } from 'ant-design-vue'
import { getImgByFileNo } from '@/services/common'
export default {
  name: 'HeaderAvatar',
  components: { AAvatar },
  data() {
    return {
      avatarUrl: ''
    }
  },
  computed: {
    ...mapGetters('account', ['user'])
  },
  inject: ['adminLayout'],
  methods: {
    ...mapActions('account', ['Logout']),
    ...mapMutations('account', ['clearStorage']),
    logout() {
      this.Logout().then(() => { this.$router.push('/login') })
    },
    getAvatar() {
      if (!this.user.avatar) {
        this.avatarUrl = ''
        return
      }
      getImgByFileNo(this.user.avatar).then(url => { this.avatarUrl = url })
    },
    toggledSetting() {
      this.adminLayout.showSetting = !this.adminLayout.showSetting
    },
    toCenter() {
      this.$router.push('/account/center')
    }
  },
  mounted() {
    this.getAvatar()
  }
}
</script>

<style lang="less">
.header-avatar{
  display: inline-flex;
  .avatar, .name{
    align-self: center;
  }
  .avatar{
    margin-right: 8px;
  }
  .name{
    font-weight: 500;
  }
}
.avatar-menu{
  width: 150px;
}

</style>
